package com.zmn.oms.business.impl.factory;

import com.zmn.base.common.data.common.dro.area.AreaDRO;
import com.zmn.base.common.data.dubbo.interfaces.area.AreaListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.map.Point;
import com.zmn.common.utils.math.MathUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.baidumap.BaiduMapBService;
import com.zmn.oms.business.interfaces.factory.OrderFactoryBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.order.OrderDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.factory.OrderFactory;
import com.zmn.oms.model.entity.order.Order;
import com.zmn.oms.model.vo.factory.NavigationDistanceVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：厂商订单
 *
 * @author liuying
 * @date 2018/11/07 19:31
 */
@Service
public class OrderFactoryBServiceImpl implements OrderFactoryBService {

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    protected AreaListRemoteService areaListRemoteService;
    private Logger logger = LoggerFactory.getLogger(OrderFactoryBServiceImpl.class);
//    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
//    private FactoryProductListRemoteService factoryProductListRemoteService;
//    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
//    private FactoryListRemoteService factoryListRemoteService;
    @Resource
    private BaiduMapBService baiduMapBService;

    @Override
    public void processOrderFactory(OrderDTO orderDTO) throws OmsBaseException {
        OrderFactory orderFactory = orderDTO.getOrderFactory();
        OrderDetail orderDetail = orderDTO.getOrderDetail();
        Order order = orderDTO.getOrder();

        if (orderFactory == null) {
            return;
        }
        throw new OmsBaseException("厂商信息不存在");
        // 获取厂商信息
        /*ResponseDTO<FactoryDRO> factoryDROResponseDTO = factoryListRemoteService.getFactoryById(orderFactory.getFactoryId());
        FactoryDRO factoryDRO = factoryDROResponseDTO.getData();
        if (factoryDRO == null) {
            throw new OmsBaseException("厂商信息不存在");
        }

        //TODO sunlingfu 产品品牌如何处理？
        orderFactory.setFcProductId(0);
        orderFactory.setFcProductName("其他");
        orderFactory.setFcBrandId(1051);
        orderFactory.setFcBrandName(StringUtils.defaultString("其他品牌"));

        // 计算远程费和远程距离，新单保内
        if (Objects.equals(orderDTO.getOrderWork().getType(), OrderConsts.ORDER_TYPE_NEW) && Objects.equals(GlobalConsts.YES, factoryDRO.getRemoteAmountFlag()) && Objects.equals(com.zmn.consts.GlobalConsts.BIZ_TYPE_F, order.getBizType())) {
            NavigationDistanceVO navigationDistanceVO = this.getNavigationDistance(orderDetail.getCountyId(), new Point(orderDetail.getLongitude(), orderDetail.getLatitude()));
            orderFactory.setRemoteAmount(Optional.ofNullable(navigationDistanceVO.getRemoteAmount()).orElse(0));
            orderFactory.setNavigationDistance(Optional.ofNullable(navigationDistanceVO.getDistance()).orElse(0));

            // 设置原价
            if (order.getOriginalAmount() != null) {
                order.setOriginalAmount(orderFactory.getRemoteAmount() + order.getOriginalAmount());
            }

        } else {
            orderFactory.setRemoteAmount(0);
            orderFactory.setNavigationDistance(0);
        }

        orderFactory.setPartReviewStatus(GlobalConsts.NONE);
        orderFactory.setPartReviewVersion(GlobalConsts.NONE);
        orderFactory.setFcPartReviewStatus(GlobalConsts.NONE);
        orderFactory.setFcPartReviewVersion(GlobalConsts.NONE);*/
    }

    @Override
    public NavigationDistanceVO getNavigationDistance(Integer areaId, Point destPoint){

        logger.info("计算远程费入参areaId[{}]，destPoint[{}]", areaId, destPoint);
        NavigationDistanceVO navigationDistanceVO = new NavigationDistanceVO();
        navigationDistanceVO.setDistance(0);
        navigationDistanceVO.setRemoteAmount(0);

        if (NumberUtil.isNullOrZero(areaId) || destPoint == null || destPoint.getLat() == null) {
            logger.warn("入参有误，不算远程费");
            return navigationDistanceVO;
        }
        ResponseDTO<AreaDRO> responseDTO = areaListRemoteService.getById(areaId);
        AreaDRO areaDRO = responseDTO.getData();
        if (areaDRO == null || areaDRO.getLongitude() == null || areaDRO.getLatitude() == null) {
            logger.warn("城市中心位置信息错误，不算远程费");
            return navigationDistanceVO;
        }

        // 获取导航距离
        Integer distance = baiduMapBService.getNavigationDistance(destPoint, new Point(areaDRO.getLongitude(), areaDRO.getLatitude()));
        if (distance == null) {
            logger.warn("未找到导航距离，不算远程费");
            return navigationDistanceVO;
        }

        // 计算远程费
        Integer remoteAmount = this.calcRemoteAmount(distance);
        navigationDistanceVO.setDistance(distance);
        navigationDistanceVO.setRemoteAmount(remoteAmount);
        return navigationDistanceVO;
    }

    /**
     * 计算远程费
     *
     * @param distance 距离 单位/米
     * @return
     */
    private Integer calcRemoteAmount(double distance) {

        // 转换成公里
        Double kmDistance = MathUtil.div(distance, 1000d, 2);
        if (kmDistance > OrderConsts.START_TOLL_KM) {

            // 超出公里数，向上取整
            Double overDistance = Math.ceil(MathUtil.sub(kmDistance, OrderConsts.START_TOLL_KM));

            // 远程费 = 超出公里数 * 每公里1.5元 ；
            return (int) (MathUtil.mul(overDistance, OrderConsts.PER_AMOUNT) * 100);

        } else {
            return 0;
        }

    }

}
